"""
平台API配置模块
用于管理不同平台的API配置和端点信息
"""

from typing import Dict, Any
from dataclasses import dataclass
from typing import Optional, Union


@dataclass
class EndpointConfig:
    """端点配置数据类"""
    path: str                           # 端点路径
    method: str = "POST"                # HTTP方法 (GET, POST等)
    param_type: str = "json"            # 参数类型 (json, query, form)


@dataclass
class PlatformConfig:
    """平台配置数据类"""
    name: str                           # 平台名称
    base_url: str                       # 基础URL
    api_key_env_var: str                # API密钥环境变量名称
    default_headers: Dict[str, str]     # 默认请求头
    endpoints: Dict[str, Union[str, EndpointConfig]]  # 端点配置


# TikHub平台配置
TIKHUB_CONFIG = PlatformConfig(
    name="tikhub",
    base_url="https://api.tikhub.io",
    api_key_env_var="TIKHUB_API_KEY",
    default_headers={
        "Content-Type": "application/json"
    },
    endpoints={
        # 视频相关
        "fetch_video_high_quality_play_url": EndpointConfig(
            path="/api/v1/douyin/app/v3/fetch_video_high_quality_play_url",
            method="GET",
            param_type="query"
        ),
        "fetch_one_video_by_share_url": EndpointConfig(
            path="/api/v1/douyin/app/v3/fetch_one_video_by_share_url",
            method="GET",
            param_type="query"
        ),
        "fetch_user_post_videos": EndpointConfig(
            path="/api/v1/douyin/app/v3/fetch_user_post_videos",
            method="GET",
            param_type="query"
        ),
        
        # 搜索相关
        "fetch_general_search_v3": EndpointConfig(
            path="/api/v1/douyin/search/fetch_video_search_v3",
            method="POST",
            param_type="json"
        ),
        "fetch_general_search_result": EndpointConfig(
            path="/api/v1/douyin/app/v3/fetch_general_search_result",
            method="GET",
            param_type="query"
        ),
        "search_tiktok_video": EndpointConfig(
            path="/api/v1/tiktok/web/search_video",
            method="GET",
            param_type="query"
        ),
        
        # Sora2相关
        "sora2_upload_image": EndpointConfig(
            path="/api/v1/sora2/upload_image",
            method="POST",
            param_type="form"
        ),
        "sora2_create_video": EndpointConfig(
            path="/api/v1/sora2/create_video",
            method="POST",
            param_type="json"
        ),
        "sora2_get_task_status": EndpointConfig(
            path="/api/v1/sora2/get_task_status",
            method="GET",
            param_type="query"
        ),
        "sora2_get_task_detail": EndpointConfig(
            path="/api/v1/sora2/get_task_detail",
            method="GET",
            param_type="query"
        ),
    }
)


# TikTok平台配置 (基于TikHub但URL中douyin替换为tiktok)
TIKTOK_CONFIG = PlatformConfig(
    name="tiktok",
    base_url="https://api.tikhub.io",
    api_key_env_var="TIKHUB_API_KEY",
    default_headers={
        "Content-Type": "application/json"
    },
    endpoints={
        # 视频相关
        "fetch_video_high_quality_play_url": EndpointConfig(
            path="/api/v1/tiktok/app/v3/fetch_video_high_quality_play_url",
            method="GET",
            param_type="query"
        ),
        "fetch_one_video_by_share_url": EndpointConfig(
            path="/api/v1/tiktok/app/v3/fetch_one_video_by_share_url",
            method="GET",
            param_type="query"
        ),
        "fetch_user_post_videos": EndpointConfig(
            path="/api/v1/tiktok/app/v3/fetch_user_post_videos",
            method="GET",
            param_type="query"
        ),
        
        # 搜索相关
        "fetch_general_search_v3": EndpointConfig(
            path="/api/v1/tiktok/app/v3/fetch_video_search_result",
            method="GET",
            param_type="query"
        ),
        "fetch_general_search_result": EndpointConfig(
            path="/api/v1/tiktok/app/v3/fetch_general_search_result",
            method="GET",
            param_type="query"
        ),
        "search_tiktok_video": EndpointConfig(
            path="/api/v1/tiktok/web/search_video",
            method="GET",
            param_type="query"
        ),
        
        # Sora2相关
        "sora2_upload_image": EndpointConfig(
            path="/api/v1/sora2/upload_image",
            method="POST",
            param_type="form"
        ),
        "sora2_create_video": EndpointConfig(
            path="/api/v1/sora2/create_video",
            method="POST",
            param_type="json"
        ),
        "sora2_get_task_status": EndpointConfig(
            path="/api/v1/sora2/get_task_status",
            method="GET",
            param_type="query"
        ),
        "sora2_get_task_detail": EndpointConfig(
            path="/api/v1/sora2/get_task_detail",
            method="GET",
            param_type="query"
        ),
    }
)


# 鬼手平台配置
GHOSTCUT_CONFIG = PlatformConfig(
    name="ghostcut",
    base_url="https://api.zhaoli.com",
    api_key_env_var="GHOSTCUT_APP_KEY",
    default_headers={
        "Content-Type": "application/json"
    },
    endpoints={
        # 视频翻译相关
        "video_translate": EndpointConfig(
            path="/v-w-c/gateway/ve/work/free",
            method="POST",
            param_type="json"
        ),
        "video_status": EndpointConfig(
            path="/v-w-c/gateway/ve/work/status",
            method="GET",
            param_type="query"
        ),
        "video_retry": EndpointConfig(
            path="/v-w-c/ve/work/retry",
            method="POST",
            param_type="json"
        ),
    }
)


# 平台配置映射
PLATFORM_CONFIGS = {
    "tikhub": TIKHUB_CONFIG,
    "tiktok": TIKTOK_CONFIG,
    "ghostcut": GHOSTCUT_CONFIG
}


def get_platform_config(platform_name: str) -> Optional[PlatformConfig]:
    """
    获取平台配置
    
    Args:
        platform_name (str): 平台名称
        
    Returns:
        PlatformConfig: 平台配置对象，如果不存在则返回None
    """
    return PLATFORM_CONFIGS.get(platform_name.lower())


def get_endpoint_config(platform_name: str, endpoint_name: str) -> Optional[EndpointConfig]:
    """
    获取平台端点配置
    
    Args:
        platform_name (str): 平台名称
        endpoint_name (str): 端点名称
        
    Returns:
        EndpointConfig: 端点配置对象，如果平台或端点不存在则返回None
    """
    config = get_platform_config(platform_name)
    if not config:
        return None
        
    endpoint = config.endpoints.get(endpoint_name)
    if not endpoint:
        return None
    
    # 如果是字符串，则转换为默认的EndpointConfig
    if isinstance(endpoint, str):
        return EndpointConfig(path=endpoint)
        
    return endpoint


def get_endpoint_url(platform_name: str, endpoint_name: str) -> Optional[str]:
    """
    获取平台端点URL
    
    Args:
        platform_name (str): 平台名称
        endpoint_name (str): 端点名称
        
    Returns:
        str: 完整的端点URL，如果平台或端点不存在则返回None
    """
    config = get_platform_config(platform_name)
    if not config:
        return None
    
    endpoint_config = get_endpoint_config(platform_name, endpoint_name)
    if not endpoint_config:
        return None
        
    return f"{config.base_url.rstrip('/')}{endpoint_config.path}"